package com.jzo2o.foundations.controller.consumer;

import com.jzo2o.foundations.model.dto.response.ServeAggregationSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeAggregationTypeSimpleResDTO;
import com.jzo2o.foundations.model.dto.response.ServeCategoryResDTO;
import com.jzo2o.foundations.model.dto.response.ServeSimpleResDTO;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IServeTypeService;
import com.jzo2o.foundations.service.ServeAggregationService;
import com.jzo2o.foundations.service.impl.HomeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户端 - 门户首页服务查询
 */
@Validated // 参数校验
@RestController("consumerServeController")
@RequestMapping("/customer/serve")
@Api(tags = "用户端 - 门户首页服务查询接口")
public class FirstPageServeController {
    @Resource
    private HomeService homeService; // 首页服务查询服务
    @Resource
    private ServeAggregationService serveAggregationService; // 首页服务聚合查询服务

    /**
     * 用户端 - 获取首页服务类型及服务项
     * 查询所有已上架的服务类型及其下属的服务项图标
     *
     * @param regionId 区域id
     * @return 首页服务列表
     */
    @GetMapping("/firstPageServeList")
    @ApiOperation(value = "首页服务列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "regionId", value = "区域id", required = true, dataTypeClass = Long.class)
    })
    public List<ServeCategoryResDTO> serveCategory(@RequestParam("regionId") Long regionId) {
        return homeService.queryServeIconCategoryByRegionIdCache(regionId);
    }

    /**
     * 用户端 - 首页全部服务：服务类型列表查询
     * 查询所有存在已上架服务的服务类型
     *
     * @param regionId 区域id
     * @return 首页服务类型列表
     */
    @GetMapping("/serveTypeList")
    @ApiOperation(value = "首页全部服务：服务类型列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "regionId", value = "区域id", required = true, dataTypeClass = Long.class)
    })
    public List<ServeAggregationTypeSimpleResDTO> getServeTypeList(@RequestParam("regionId") Long regionId) {
        return homeService.getServeTypeList(regionId);
    }


    /**
     * 用户端 - 首页热门服务列表
     * 查询条件：区域id。是否热门（是）、是否上架（是）
     */
    @GetMapping("/hotServeList")
    @ApiOperation(value = "首页热门服务列表")
    public List<ServeAggregationSimpleResDTO> hotServeList(@NotNull(message = "regionId不能为空") @RequestParam("regionId") Long regionId) {
        return homeService.queryHotServeListByRegionIdCache(regionId);
    }

    /**
     * 用户端 - 服务详情，根据id查询服务详情
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "服务详情")
    public ServeAggregationSimpleResDTO serveDetail(@NotNull(message = "id不能为空") @PathVariable("id") Long id) {
        return homeService.queryServeDetail(id);
    }


    /**
     * 用户端 - 首页服务搜索
     * 搜索条件：城市编码、服务类型id、关键词
     */
    @GetMapping("/search")
    @ApiOperation("首页服务搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cityCode", value = "城市编码", required = true, dataTypeClass = String.class),
            @ApiImplicitParam(name = "serveTypeId", value = "服务类型id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "keyword", value = "关键词", dataTypeClass = String.class)
    })
    public List<ServeSimpleResDTO> findServeList(@RequestParam("cityCode") String cityCode,
                                                 @RequestParam(value = "serveTypeId", required = false) Long serveTypeId,
                                                 @RequestParam(value = "keyword", required = false) String keyword) {
        return serveAggregationService.findServeList(cityCode, serveTypeId, keyword);
    }

}
